package com.shiyingzi.security.service.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;



import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;

import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.shiyingzi.auser.model.AChannel;
import com.shiyingzi.common.EntityUtils;
import com.shiyingzi.common.PageUtils;
import com.shiyingzi.common.PageableData;
import com.shiyingzi.common.ResponseData;
import com.shiyingzi.security.dao.UserDao;
import com.shiyingzi.security.model.User;
import com.shiyingzi.security.service.IUserService;
import com.shiyingzi.utils.JdbcTemplateUtils;

@Service(IUserService.BEAN_ID)
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements IUserService{

	@Autowired
	private UserDao userDao;

	@Autowired
	private HttpSession session;
	@Override
	public ResponseData insertUser(User user) {
		
		try{
			user.setDeleteFlag("N");
			return new ResponseData(true,"true",userDao.insert(user));
		}catch(Exception e){
			return new ResponseData(false,"false");
		}
	}

	@Override
	public ResponseData updateUser(User user) {
		
		try{			
			return new ResponseData(true,"true",userDao.updateById(user));
		}catch(Exception e){
			return new ResponseData(false,"false");
		}
		
	}

	@Override
	public ResponseData deleteUser(String userName) {
		try{ 
			List<User> user = userDao.selectList(new EntityWrapper<User>().eq("user_name", userName));
			List ids = new ArrayList<>();
			for(User user1 :user){
				ids.add(user1.getId());
			}
			userDao.deleteBatchIds(ids);
			return new ResponseData(true,"true");
		}catch(Exception e){
			return new ResponseData(false,"false");
		}
	}

	@Override
	public ResponseData queryUser(String userName) {
//		try{
//			return new ResponseData(true,"true",userDao.findByUserName(userName));
//		}catch(Exception e){
//			return new ResponseData(false,"false");
//		}
		
		
		try{
			String sql = "select * from _user where user_name = '";
			sql += userName + "';";
			User user = JdbcTemplateUtils.getTemplate().query(sql, new ResultSetExtractor<User>() {
				public User extractData(ResultSet set) throws SQLException,DataAccessException {
					User user = new User();
					user.setUserName(set.getString("user_name"));
					user.setUserAccout(set.getString("user_accout"));
					user.setUserPassword(set.getString("user_password"));
					return user;
				}
			});
			
			return new ResponseData(true,"true",user);
		}catch(Exception e){
			return new ResponseData(false,"false");
		}
	}

	@Override
	public PageableData<User> queryUserList(int page, int limit, String userName) {
		
		Page<User> mypage = new Page<User>();
		mypage.setCurrent(page);
		mypage.setSize(limit);
		User ac =new User();
		if(!StringUtils.isEmpty(userName)){
			ac.setUserName(userName);
		}
		
		return PageUtils.tableDataDto(selectPage(mypage,new EntityWrapper<User>(ac).orderBy("createTime")));
	}

	@Override
	public ResponseData updateUser2(User user) {
		try{
			EntityUtils.modifyEntity(user, session);
			userDao.insert(user);
			return new ResponseData(true,"true",user);
		}catch(Exception e){
			return new ResponseData(false,"false");
		}
	}
}
